package com.mdtx.controller;

import com.mdtx.entity.TableAttribute;
import org.json.JSONArray;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author zhgz
 * @create 2017-07-29 16:43
 **/
public class Test {
    private static final String TABLE_REMARKS = "REMARKS";
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/csdata?useUnicode=true&characterEncoding=UTF-8";
        String user = "root";
        String password = "123456";

        Properties props = new Properties();
        props.setProperty("user", user);
        props.setProperty("password", password);
        props.setProperty("remarks", "true"); //设置可以获取remarks信息
        props.setProperty("useInformationSchema", "true");//设置可以获取tables remarks信息


        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url,props);

        if (!conn.isClosed())
            System.out.println("Succeeded connecting to the Database!");
        else
            System.err.println("connect filed");

        Statement state = conn.createStatement();

        ResultSet rs = state.executeQuery("show databases");

        ResultSetMetaData metaData = rs.getMetaData();
        while (rs.next()) {

            for (int i = 0; i < metaData.getColumnCount(); i++) {
               // String cols_name = metaData.getColumnName()
              //  System.out.println(rs.getObject(i+1));


            }

        }

        getAllTabes(conn);
       // getAllColumns(conn);
    }

    public  static void getAllTabes(Connection conn) throws SQLException {
        DatabaseMetaData metaData = conn.getMetaData();

      //  ResultSet resultSet = metaData.getTables(null,"%","%",new String[]{"TABLE"});
        ResultSet resultSet = metaData.getTables(null, "csdata", "%", new String[] { "TABLE" });
        while (resultSet.next()){
         //   System.out.println(resultSet.getString(TABLE_REMARKS));
            /*if (resultSet.getString(4) != null
                    && (resultSet.getString(4).equalsIgnoreCase("TABLE") )) {
                String tableName = resultSet.getString(3).toLowerCase();
                System.out.println(resultSet.getString("TABLE_NAME") );


            }*/

            while (resultSet.next()) {
                String tableName=resultSet.getString("TABLE_NAME");
                String remarkes = resultSet.getString("REMARKS");
                System.out.println(tableName+"="+remarkes);
            }
            }
    }

    public  static void getAllColumns(Connection conn) throws SQLException {
       DatabaseMetaData metaData = conn.getMetaData();
        //ResultSet colRet = metaData.getColumns(null,"csdata", "kh_jgxx","%");
        ResultSet colRet=metaData.getPrimaryKeys(null,"csdata","kh_jgxx");

        while (colRet.next()) {
            String  columnName = colRet.getString("COLUMN_NAME");
         //   String columnType = colRet.getString("TYPE_NAME");
         //   int datasize = colRet.getInt("COLUMN_SIZE");
         //   int digits = colRet.getInt("DECIMAL_DIGITS");
       //    int nullable = colRet.getInt("NULLABLE");
         //   String comment =  colRet.getString("REMARKS");
            System.out.println(columnName);// + " " + columnType + " " + datasize + " " + digits + " " + nullable);
        }

       /* List<TableAttribute> list = new ArrayList<TableAttribute>();
        TableAttribute t = new TableAttribute();
        t.setComment("测试");
        t.setTableName("kh_jgxx");

        System.out.println();*/
    }

}
